package com.skt.TFriend.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.skt.TFriend.R;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.ArrayAdapter; 

public class Weather_Info extends ListActivity  {

	ArrayAdapter<String> mArrayAdapter;	    
	List<String> mItems = new ArrayList<String>();	    
	ListView mListView; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather_info);    
        
        mListView = (ListView)findViewById(android.R.id.list);
        setCalendar();
    }
    
    public void setCalendar(){
    	Uri calendars = Uri.parse("content://com.android.calendar/events");
    	String[] projection = new String[] {
    	"calendar_id",      
    	"htmlUri",          
    	"title",            
    	"eventLocation",    
    	"description",      
    	"eventStatus",      
    	"selfAttendeeStatus",
    	"commentsUri",      
    	"dtstart",          
    	"dtend",            
    	"eventTimezone",    
    	"duration",         
    	"allDay",           
    	"visibility",       
    	"transparency",     
    	"hasAlarm",         
    	"hasExtendedProperties",
    	"rrule",            
    	"rdate",            
    	"exrule",           
    	"exdate",           
    	"originalEvent",    
    	"originalInstanceTime",
    	"originalAllDay",   
    	"lastDate",         
    	"hasAttendeeData",  
    	"guestsCanModify",  
    	"guestsCanInviteOthers",
    	"guestsCanSeeGuests",
    	"organizer",        
    	"deleted"
    	};
    	
    	Date dt1 = new Date();	
    	// 오늘 (today 00시00분00초 ~ 23시59분59초)  (23*60*60*1000) + (59*60*1000) + (59*1000) + 999 = 86399999
    	Date today = new Date(dt1.getYear(),dt1.getMonth(),dt1.getDate());
    	
    	Log.i("Calendar", "today.getTime() =>"+today.getTime());
    	long s_today = today.getTime();
    	long e_today = s_today + 86399999;		
    	// 쿼리 조건 셋팅    	
    	String selections = "selected=1 AND dtstart between "+s_today+" and " + e_today;
    	Log.i("Calendar", "selections : " + selections);
    	Cursor managedCursor = getContentResolver().query(calendars, projection, selections, null, null) ;
    	
    	if(managedCursor.moveToFirst()) {
    		int[] calendar_id               = new int[managedCursor.getCount()];            
    		String[] htmlUri                = new String[managedCursor.getCount()];
    		String[] title                  = new String[managedCursor.getCount()];
    		String[] eventLocation          = new String[managedCursor.getCount()];
    		String[] description            = new String[managedCursor.getCount()];
    		int[] eventStatus               = new int[managedCursor.getCount()];
    		int[] selfAttendeeStatus        = new int[managedCursor.getCount()];
    		String[] commentsUri            = new String[managedCursor.getCount()];
    		long[] dtstart                = new long[managedCursor.getCount()];
    		int[] dtend                  = new int[managedCursor.getCount()];
    		String[] eventTimezone          = new String[managedCursor.getCount()];
    		String[] duration               = new String[managedCursor.getCount()];
    		int[] allDay                    = new int[managedCursor.getCount()];
    		int[] visibility                = new int[managedCursor.getCount()];
    		int[] transparency              = new int[managedCursor.getCount()];
    		int[] hasAlarm                  = new int[managedCursor.getCount()];
    		int[] hasExtendedProperties     = new int[managedCursor.getCount()];
    		String[] rrule                  = new String[managedCursor.getCount()];
    		String[] rdate                  = new String[managedCursor.getCount()];
    		String[] exrule                 = new String[managedCursor.getCount()];
    		String[] exdate                 = new String[managedCursor.getCount()];
    		String[] originalEvent          = new String[managedCursor.getCount()];
    		int[] originalInstanceTime      = new int[managedCursor.getCount()];
    		int[] originalAllDay            = new int[managedCursor.getCount()];
    		int[] lastDate               = new int[managedCursor.getCount()];
    		int[] hasAttendeeData           = new int[managedCursor.getCount()];
    		int[] guestsCanModify           = new int[managedCursor.getCount()];
    		int[] guestsCanInviteOthers     = new int[managedCursor.getCount()];
    		int[] guestsCanSeeGuests        = new int[managedCursor.getCount()];
    		String[] organizer              = new String[managedCursor.getCount()];
    		int[] deleted                   = new int[managedCursor.getCount()];            
    		for (int i = 0 ; i < title.length ; i++) {
    		calendar_id[i] = managedCursor.getInt(0);
    		Log.i("Calendar", "ID : " + calendar_id[i]);
    		htmlUri[i] = managedCursor.getString(1);
    		Log.i("Calendar", "htmlUri : " + htmlUri[i]);
    		title[i] = managedCursor.getString(2);
    		Log.i("Calendar", "title : " + title[i]);
    		eventLocation[i] = managedCursor.getString(3);
    		Log.i("Calendar", "eventLocation : " + eventLocation[i]);
    		description[i] = managedCursor.getString(4); 
    		Log.i("Calendar", "description : " + description[i]);
    		eventStatus[i] = managedCursor.getInt(5);
    		Log.i("Calendar", "eventStatus : " + eventStatus[i]);
    		selfAttendeeStatus[i] = managedCursor.getInt(6);
    		Log.i("Calendar", "selfAttendeeStatus : " + selfAttendeeStatus[i]);
    		commentsUri[i] = managedCursor.getString(7);
    		Log.i("Calendar", "commentsUri : " + commentsUri[i]);
    		dtstart[i] = managedCursor.getLong(8);
    		Log.i("Calendar", "dtstart : " + dtstart[i]);
    		dtend[i] = managedCursor.getInt(9);
    		Log.i("Calendar", "dtend : " + dtend[i]);
    		eventTimezone[i] = managedCursor.getString(10);
    		Log.i("Calendar", "eventTimezone : " + eventTimezone[i]);
    		duration[i] = managedCursor.getString(11);
    		Log.i("Calendar", "duration : " + duration[i]);
    		allDay[i] = managedCursor.getInt(12);
    		Log.i("Calendar", "allDay : " + allDay[i]);
    		visibility[i] = managedCursor.getInt(13);
    		Log.i("Calendar", "visibility : " + visibility[i]);
    		transparency[i] = managedCursor.getInt(14);
    		Log.i("Calendar", "transparency : " + transparency[i]);
    		hasAlarm[i] = managedCursor.getInt(15);
    		Log.i("Calendar", "hasAlarm : " + hasAlarm[i]);
    		hasExtendedProperties[i] = managedCursor.getInt(16);
    		Log.i("Calendar", "hasExtendedProperties : " + hasExtendedProperties[i]);
    		rrule[i] = managedCursor.getString(17);
    		Log.i("Calendar", "rrule : " + rrule[i]);
    		rdate[i] = managedCursor.getString(18);    		
    		Log.i("Calendar", "rdate : " + rdate[i]);
    		exrule[i] = managedCursor.getString(19);
    		Log.i("Calendar", "exrule : " + exrule[i]);
    		exdate[i] = managedCursor.getString(20);
    		Log.i("Calendar", "exdate : " + exdate[i]);
    		originalEvent[i] = managedCursor.getString(21);
    		Log.i("Calendar", "originalEvent : " + originalEvent[i]);
    		originalInstanceTime[i] = managedCursor.getInt(22);
    		Log.i("Calendar", "originalInstanceTime : " + originalInstanceTime[i]);
    		originalAllDay[i] = managedCursor.getInt(23);
    		Log.i("Calendar", "originalAllDay : " + originalAllDay[i]);
    		lastDate[i] = managedCursor.getInt(24);
    		Log.i("Calendar", "lastDate : " + lastDate[i]);
    		hasAttendeeData[i] = managedCursor.getInt(25);
    		Log.i("Calendar", "hasAttendeeData : " + hasAttendeeData[i]);
    		guestsCanModify[i] = managedCursor.getInt(26);
    		Log.i("Calendar", "hasAttendeeData : " + hasAttendeeData[i]);
    		guestsCanInviteOthers[i] = managedCursor.getInt(27);
    		Log.i("Calendar", "guestsCanInviteOthers : " + guestsCanInviteOthers[i]);
    		guestsCanSeeGuests[i] = managedCursor.getInt(28);
    		Log.i("Calendar", "guestsCanSeeGuests : " + guestsCanSeeGuests[i]);
    		organizer[i] = managedCursor.getString(29);
    		Log.i("Calendar", "organizer : " + organizer[i]);
    		deleted[i] = managedCursor.getInt(30);
    		Log.i("Calendar", "deleted : " + deleted[i]);
    		if (title[i] != null)
    		{                    
    			mItems.add(title[i]);
    		}                
    		managedCursor.moveToNext();
    		}            
    		managedCursor.close();            
    		mArrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, mItems);
    		mListView = (ListView)findViewById (android.R.id.list);
    		mListView.setAdapter(mArrayAdapter);
    		mListView.setItemsCanFocus(true); 
    	}
    }
}
